#include "hud.h"

HUD::HUD(Player* p1, Player* p2, Player* p3, Player* p4)
{
	round = 0;
	players[0] = p1;
	players[1] = p2;
	players[2] = p3;
	players[3] = p4;
	spPoc = 0;
	imgLights[0].LoadFromFile("data/off.png");
	imgLights[1].LoadFromFile("data/red.png");
	imgLights[2].LoadFromFile("data/green.png");
	imgLights[0].SetSmooth(false);
	imgLights[1].SetSmooth(false);
	imgLights[2].SetSmooth(false);
}

void HUD::start()
{
	clock.Reset();	
	round++;
	state = HS_OFF;
	lights = 0;	
	place = 0;
	spLights[0].SetImage(imgLights[0]);	
	spLights[1].SetImage(imgLights[0]);	
	spLights[2].SetImage(imgLights[0]);
}

void HUD::update()
{
	if (state == HS_OVER)
	{
		if (p1->getPlace() == 0 && p1->getState() == PS_GROUND)	p1->setPlace(++place);	
		if (p2->getPlace() == 0 && p2->getState() == PS_GROUND)	p2->setPlace(++place);	
		if (p3->getPlace() == 0 && p3->getState() == PS_GROUND)	p3->setPlace(++place);	
		if (p4->getPlace() == 0 && p4->getState() == PS_GROUND)	p4->setPlace(++place);
		if (p1->getState() >= PS_DEAD && p2->getState() >= PS_DEAD &&
		    p3->getState() >= PS_DEAD && p4->getState() >= PS_DEAD)
		{
			if (p1->)p1->setPoints();
			state = HS_NEWROUND;			
		}
	}
	else
	{
		if (clock.GetElapsedTime() > 1.0f)
		{
			if (lights == 3) state = HS_GREEN;
			if (state == HS_GREEN)	
			{
				spLights[0].SetImage(imgLights[2]);	
				spLights[1].SetImage(imgLights[2]);	
				spLights[2].SetImage(imgLights[2]);	
			}
			if (state == HS_OFF) state = HS_RED;
			if (state == HS_RED) spLights[lights++].SetImage(imgLights[1]);
			clock.Reset();
		}
	}
}

bool HUD::nextRound()
{
	if (state == HS_OVER) return true;
	else return false;
}

bool HUD::gameOver()
{
	if (state == HS_GAMEOVER) return true;
	else return false;
}

sf::Sprite* HUD::getSprite(int height)
{	
	spLights[0].SetPosition(210, height - 2150);	
	spLights[1].SetPosition(300, height - 2150);	
	spLights[2].SetPosition(390, height - 2150);	
	switch (spPoc++)
	{
	case 0: return &spLights[0]; break;
	case 1: return &spLights[1]; break;
	case 2: return &spLights[2]; break;
	case 3: spPoc = 0; return NULL; break;
	}
}

unsigned char HUD::getState()
{
	return state;
}

HUD::~HUD()
{
}
